import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 
 * @author PUSS1401
 * 
 */
@WebServlet("/newPassword")
public class servletNewPassword extends servletBase {
	private static final long serialVersionUID = 1L;
	private static final int PASSWORD_LENGTH = 6;

	
	
	/**
	 * A servlet that handles the creation of a new randomly generated password. Extends servletBase.
	 * First it is checked that an user is logged in, otherwise the user is redirected to the login page.
	 */
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		if (session == null) {
			response.sendRedirect("lostSession.html");
			return;
		}
		if (!loggedIn(request)) {
			response.sendRedirect("LogIn");
		} else {

			HtmlGenerator gen = new HtmlGenerator();
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			String project = "";
			String role = "";
			String user = session.getAttribute("name").toString();
			if(!(session.getAttribute("project").equals("noproject"))){
				project = session.getAttribute("project").toString();
				role = session.getAttribute("role").toString();
			}

			
			String newPassword = createPassword();
			String check = request.getParameter("check");

			out.print(gen.getTop(user, project, role));
			out.println(gen.getMenu());
			out.println(changePassForm());

			if (check != null) {
				if (check.equals("yes")) {
					if (db.updatePassword(user, newPassword)) {
						out.println("<br/> Your new password is: "
								+ newPassword);
					} else {
						out.println("<br/> Your password could not be changed. Please try again.");
					}
				} else {
					out.println("You must choose 'Yes' to change password.");
				}
			}

			out.print(gen.getBot());
			out.close();
		}
	}
	

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
	/**
	 * Creates a string that shows the form for password change.
	 * 
	 * @return The HTML-code of the password form
	 */
	private String changePassForm() {
		String html = "<form name=" + formElement("newPassword");
		html += " method=" + formElement("get") + "> ";

		html += "Do you want to change your password? <br/>";

		html += "<select name =\"check\">";
		html += "<option value=\"yes\">Yes</option>";
		html += "<option value=\"no\" selected>No</option>";
		html += "</select>";

		html += " <input type=" + formElement("submit");
		html += " value=\"Change Password\">";
		html += " </form>";
		return html;
	}
	/**
	 * Generates a random password.
	 * @return String, of the newly generated password
	 */
	private String createPassword() {
		String result = "";
		Random r = new Random();
		for (int i = 0; i < PASSWORD_LENGTH; i++)
			result += (char) (r.nextInt(26) + 97); // 122-97+1=26
		return result;
	}

}
